<template>
    <div class="login_container">
        <el-row class="login_box">
            <el-col :span="14"></el-col>
            <el-col :span="10">
                <el-form class="login_from" :model="loginFrom" :rules="rules" ref="loginFormRef">
                    <h1>Logo</h1>
                    <h2>欢迎登录小陌优选后台</h2>
                   <el-form-item prop="username">
                        <el-input v-model="loginFrom.username" placeholder="请输入用户名"  :prefix-icon="User">
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input  v-model="loginFrom.password" :type="showPassword?'password':'text' " placeholder="请输入密码" :prefix-icon="Lock" show-password>
                        </el-input>
                    </el-form-item>
                    <div class="tips">
                        <span >忘记密码: 请联系系统管理员！！！</span>
                    </div>
                    <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:10px;" @click="userLogin" >登 录</el-button>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script setup lang="ts">    
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref, inject} from 'vue'
import useUserStore from '@/store/modules/user'
import { ElNotification } from 'element-plus'
import {useRouter,useRoute} from 'vue-router'

const uuidInstance:any = inject('uuidInstance');
//获取el-form表单
const loginFormRef = ref()

console.log(uuidInstance.userDefinedUUID('luozhiquan'));

//获取路由
const $router = useRouter()
const $route = useRoute()
//定义按钮加载效果
let loading = ref(false)
//引入用户相关的小仓库
let useStore = useUserStore()
let showPassword = ref(true)

const loginFrom = reactive({
    username: 'admin',
    password: '1234abc'
})

// 获取当前时间结果,返回早上|上午|中午|下午|晚上
const getTime = () => {
    // 通过内置构造函数Date
    let hours = new Date().getHours();
    let period;
    // 判断当前时间属于哪个时间段
    switch (true) {
        case hours >= 0 && hours <= 5:
            period = "晚上";
            break;
        case hours > 5 && hours <= 7:
            period = "早上";
            break;
        case hours > 7 && hours <= 11:
            period = "上午";
            break;
        case hours > 11 && hours <= 13:
            period = "中午";
            break;
        case hours > 13 && hours <= 17:
            period = "下午";
            break;
        default:
            period = "晚上";
    }
    return period;
}
//登录按钮回调
const userLogin = async () => {
    //保证全部表单校验通过再提交表单
    await loginFormRef.value.validate();
    //加载效果：开始加载
    loading.value = true
    //1.通知仓库发登录请求
    //2.请求成功->路由跳转至首页->首页展示数据
    //3.请求失败->弹出登录失败信息
     try {
        await  useStore.userLogin(loginFrom)
         const redirect = $route.query.redirect;
         if (typeof redirect === 'string') {
             $router.push({ path: redirect });
         } else {
             $router.push({ name: 'Layout' });
         }
        // $router.push({
        //     name:"Layout"
        // }    
        // )
        ElNotification({
            type:'success',
            message:'登录成功',
            title:`HI! ${loginFrom.username} ${getTime()}好`
        })
        loading.value = false
     }catch(err){
        //登录失败的提示信息
        ElNotification({
            type:'error',
            message:(err as Error).message
        })
        loading.value = false
     }  
}
//自定义校验规则函数 
//rule:即为校验规则对象
//value:即为表单元素文本内容
//函数:如果符合条件callBack放行通过即为
//如果不符合条件callBack方法,注入错误提示信息
const validateUsername = (_rule:any,value:any,callback:any)=>{
    if(value.length<3||value.length>10){
        callback(new Error('用户名长度至少为3位，至多10位'))
    }else{
        callback()
    }
}
//定义表单校验需要的配置对象
const rules = reactive({    
    //trigger:触发校验表单的时机 change->文本发生变化触发校验,blur:失去焦点的时候触发校验规则
    username:[
        // {required:true,message:'请输入用户名',trigger:'blur'},
        // {min:3,max:10,message:'用户名长度至少为3位，至多10位',trigger:'blur'}
        {trigger:'change',validator:validateUsername}
    ],
    password:[
        {required:true,message:'请输入密码',trigger:'blur'},
        {min:6,message:'密码长度至少为6位',trigger:'blur'}
    ]
})
</script>

<style scoped lang="scss">
    .login_container {
        width: 100%;
        height: 100vh;
        background:url(@/assets/images/login_bg.jpg) no-repeat center;
        background-size: cover;
        .login_from{
            position: relative;
            color: white;
            width: 48%;
            top: 28vh;
            background:url(@/assets/images/login_form.png) no-repeat center;
            background-color: rgba(0, 27, 142, 0.2);
            background-size: cover;
            padding: 40px 20px;
            border-radius:10px;
            h1{
                color: white;
                font-size: 40px;
            }
            h2{
                font-size: 20px;
                color: white;
                margin:20px 0px;

            }
            .tips {
                font-size: 13px;
                color: #fff;
                margin-bottom: 48px;
                span {
                    &:first-of-type {
                        margin-right: 16px;
                    }
                }
            }
        }
    }
</style>